package com.movie.act;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.movie.bean.MovieNew;
import com.movie.bean.MovieNewList;
import com.movie.bean.YouHuiNew;
import com.movie.cache.AsyncImageLoader;
import com.movie.task.MainActTask;
import com.movie.util.Log;
import com.movie.util.RoateUtil;

/**
 * 
 * @author
 * 
 */
public class MainAct extends BaseActivity implements MainActTask.UpdateListView {
	/** Called when the activity is first created. */
	private Context mContext = MainAct.this;

	private TextView buyticket = null;

	private LinearLayout movienamel = null;
	private LinearLayout scoreratel = null;
	private LinearLayout bottom = null;

	public void release() {
		moviefast = null;
		nearmovie = null;
		setting = null;
		currentTime = null;
		net = null;
		moviename = null;
		buyticket = null;
		scorerate = null;
		scoretext = null;
		pinluntext = null;
	}

	public void setValue() {
		// moviefast.setText( bean.moviefast );
		// nearmovie.setText( bean.nearmovie );
		// setting.setText( bean.setting );
		// currentTime.setText( bean.currentTime );
		// net.setText( bean.net );
		// moviename.setText( bean.moviename );
		// buyticket.setText( bean.buyticket );
		// scorerate.setText( bean.scorerate );
		// scoretext.setText( bean.scoretext );
		// pinluntext.setText( bean.pinluntext );

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mAsyncImageLoader = new AsyncImageLoader();
		findViewByIds();
		initTitleBar();

		mMovieNewList = new MovieNewList();
		mMainActTask = new MainActTask(this, mMovieNewList);
		mMainActTask.execute();

		// RoateUtil.tranlate(mContext, bottom);

		onTitleClick();

		buyticket.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 进到购票页面
				Log.i("----------buyticket-onclick");
				Intent intent = new Intent(mContext, BuyTicketAct.class);
				mContext.startActivity(intent);
			}
		});
		RoateUtil.tranlate(mContext, pinluntext);
		RoateUtil.tranlate(mContext, scoreratel);
		RoateUtil.tranlate(mContext, movienamel);
	}

	private TextView moviefast = null;
	private TextView nearmovie = null;

	private void onTitleClick() {
		moviefast = (TextView) findViewById(R.id.moviefast);
		nearmovie = (TextView) findViewById(R.id.nearmovie);

		nearmovie.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 进到周边电影
				Intent intent = new Intent(mContext, MovieCinemaAct.class);
				mContext.startActivity(intent);
			}
		});
	}

	private void findViewByIds() {

		bigimage = (ImageView) findViewById(R.id.bigimage);
		moviename = (TextView) findViewById(R.id.moviename);
		buyticket = (TextView) findViewById(R.id.buyticket);
		scorerate = (RatingBar) findViewById(R.id.scorerate);
		scoretext = (TextView) findViewById(R.id.scoretext);
		pinluntext = (TextView) findViewById(R.id.pinluntext);
		movienamel = (LinearLayout) findViewById(R.id.movienamel);
		scoreratel = (LinearLayout) findViewById(R.id.scoreratel);
		// bottom = (LinearLayout) findViewById(R.id.bottom);
	}

	private RatingBar scorerate = null;
	private TextView scoretext = null;
	private TextView pinluntext = null;
	private ImageView bigimage = null;
	private TextView moviename = null;

	private void showMovieItem(MovieNew bean) {
		Log.i("-----MainAct.showMovieItem-" + bean.point);
		scorerate.setRating(Float.parseFloat(bean.scorerate));
		scoretext.setText(bean.point);
		pinluntext.setText(bean.pinluntext);
		moviename.setText(bean.name);
		// bigimage.setImageResource(bean.pinluntext);
	}

	// -------------------影片列表-----------START
	public void initListView() {
		initMovieNewListView();

		initYouHuiListView();
		initGallery();
	}

	public class MovieNewGalleryAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public MovieNewGalleryAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return mMovieNewList.mMovieNews.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final Galleryitem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.movie_new_gallery_item, null);
				holder = new Galleryitem();
				holder.imageUrl = (ImageView) convertView
						.findViewById(R.id.imageurl);
				convertView.setTag(holder);
			} else {
				holder = (Galleryitem) convertView.getTag();
			}
			if (position % 2 == 0) {
				holder.imageUrl.setImageResource(R.drawable.default_bg);
			} else {
				holder.imageUrl.setImageResource(R.drawable.imagetemp);
			}
			return convertView;
		}
	}

	private com.movie.view.UGallery main_gallery = null;
	private MovieNewGalleryAdapter mMovieNewGalleryAdapter = null;

	private void initGallery() {
		main_gallery = (com.movie.view.UGallery) findViewById(R.id.main_gallery);
		mMovieNewGalleryAdapter = new MovieNewGalleryAdapter();
		main_gallery.setAdapter(mMovieNewGalleryAdapter);
		main_gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> mAdapterView, View view,
					int position, long id) {
				showMovieItem(mMovieNewList.mMovieNews.get(position));
			}

			@Override
			public void onNothingSelected(AdapterView<?> mAdapterView) {

			}
		});
	}

	private static class Galleryitem {
		public ImageView imageUrl;
	}

	private void initMovieNewListView() {
		more_listview = (ListView) findViewById(R.id.more_listview);
		mMovieNewListAdapter = new MovieNewListAdapter();
		more_listview.setAdapter(mMovieNewListAdapter);

		listview_menu = (ImageView) findViewById(R.id.listview_menu);
		listview_menu.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (ismListViewHidden) {
					more_listview.setVisibility(View.VISIBLE);
					youhui_listview_menu.setVisibility(View.INVISIBLE);
					new_btn.setVisibility(View.INVISIBLE);
					ismListViewHidden = false;
				} else {
					more_listview.setVisibility(View.GONE);
					ismListViewHidden = true;
					youhui_listview_menu.setVisibility(View.VISIBLE);
					new_btn.setVisibility(View.VISIBLE);
				}

			}
		});
	}

	private ImageView listview_menu = null;
	private boolean ismListViewHidden = true;
	private ListView more_listview = null;
	private MovieNewListAdapter mMovieNewListAdapter = null;
	private MovieNewList mMovieNewList = null;
	private MainActTask mMainActTask = null;
	public static AsyncImageLoader mAsyncImageLoader = null;

	public class MovieNewListAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public MovieNewListAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return mMovieNewList.mMovieNews.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderListViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.movie_new_list_item, null);
				holder = new ViewHolderListViewItem();
				holder.imageUrl = (ImageView) convertView
						.findViewById(R.id.imageUrl);
				holder.point = (TextView) convertView.findViewById(R.id.point);
				holder.pointbg = (TextView) convertView
						.findViewById(R.id.pointbg);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.name_orange = (TextView) convertView
						.findViewById(R.id.name_orange);
				holder.date_l = (LinearLayout) convertView
						.findViewById(R.id.date_l);
				RoateUtil.tranlateCenter(mContext, holder.point);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderListViewItem) convertView.getTag();
				holder.point.setVisibility(View.INVISIBLE);
				holder.pointbg.setVisibility(View.INVISIBLE);
				holder.date_l.setVisibility(View.INVISIBLE);
			}
			MovieNew item = mMovieNewList.mMovieNews.get(position);
			String url = item.imageUrl;

			// ImageView cacheImage = holder.imageUrl;
			// holder.imageUrl.setTag(item.imageUrl);
			// MainAct.mAsyncImageLoader.getListViewLogoItem(mContext, url,
			// cacheImage, more_listview);

			holder.point.setText(item.point);
			holder.date.setText(item.date);
			holder.name.setText(item.name);
			holder.name_orange.setText(item.name);

			if ("0".equals(item.status))// 已上映
			{
				holder.date_l.setVisibility(View.INVISIBLE);

				holder.point.setVisibility(View.VISIBLE);
				holder.pointbg.setVisibility(View.VISIBLE);
				if (mMovieNewList.index == position) {
					holder.name_orange.setVisibility(View.VISIBLE);
					holder.name.setVisibility(View.GONE);
				} else {
					holder.name_orange.setVisibility(View.GONE);
					holder.name.setVisibility(View.VISIBLE);

				}
			}

			else if ("1".equals(item.status))// 未上映
			{
				holder.name.setVisibility(View.GONE);
				holder.name_orange.setVisibility(View.GONE);
				holder.date_l.setVisibility(View.VISIBLE);
				holder.point.setVisibility(View.INVISIBLE);
				holder.pointbg.setVisibility(View.INVISIBLE);
			}

			// if (selected != -1) {
			// if (selected == position) {
			// holder.name.setTextColor(0xB35201);
			// } else {
			// holder.name.setTextColor(0xB1AFAD);
			// }
			// }

			return convertView;
		}
	}

	private static class ViewHolderListViewItem {

		public ImageView imageUrl;
		public TextView point;
		public TextView pointbg;
		public TextView date;
		public TextView name;
		public TextView name_orange;

		public LinearLayout date_l;
	}

	// -------------------影片列表----------END

	// -------------------优惠列表-----------START
	public void initYouHuiListView() {
		youhui_more_listview = (ListView) findViewById(R.id.youhui_more_listview);
		mYouHuiNewListAdapter = new YouHuiNewListAdapter();
		youhui_more_listview.setAdapter(mYouHuiNewListAdapter);
		//
		// listview_menu = (ImageView) findViewById(R.id.listview_menu);
		// listview_menu.setOnClickListener(new OnClickListener() {
		//
		// public void onClick(View v) {
		// if (ismListViewHidden) {
		// youhui_more_listview.setVisibility(View.VISIBLE);
		// ismListViewHidden = false;
		// } else {
		// youhui_more_listview.setVisibility(View.GONE);
		// ismListViewHidden = true;
		// }
		//
		// }
		// });
		youhui_listview_menu = (ImageView) findViewById(R.id.youhui_listview_menu);
		new_btn = (ImageView) findViewById(R.id.new_btn);
		new_btn.setVisibility(View.VISIBLE);
		youhui_listview_menu.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (isyouhuimListViewHidden) {
					youhui_more_listview.setVisibility(View.VISIBLE);
					listview_menu.setVisibility(View.GONE);
					isyouhuimListViewHidden = false;
				} else {
					youhui_more_listview.setVisibility(View.GONE);
					listview_menu.setVisibility(View.VISIBLE);
					isyouhuimListViewHidden = true;
				}

			}
		});

	}

	private ImageView youhui_listview_menu = null;
	private ImageView new_btn = null;

	private boolean isyouhuimListViewHidden = true;
	private ListView youhui_more_listview = null;
	private YouHuiNewListAdapter mYouHuiNewListAdapter = null;

	public class YouHuiNewListAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public YouHuiNewListAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return mMovieNewList.mYouHuiNews.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.youhui_movie_new_list_item, null);
				holder = new ViewHolder();
				holder.imageUrl = (ImageView) convertView
						.findViewById(R.id.imageUrl);
				holder.logo = (ImageView) convertView.findViewById(R.id.logo);
				holder.address = (TextView) convertView
						.findViewById(R.id.address);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.name2 = (TextView) convertView.findViewById(R.id.name2);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			YouHuiNew bean = mMovieNewList.mYouHuiNews.get(position);
			String url = bean.imageUrl;

			// ImageView cacheImage = holder.imageUrl;
			// holder.imageUrl.setTag(item.imageUrl);
			// MainAct.mAsyncImageLoader.getListViewLogoItem(mContext, url,
			// cacheImage, youhui_more_listview);

			// holder.imageUrl.setText(item.imageUrl);
			// holder.logo.setText(bean.logo);
			holder.address.setText(bean.address);
			holder.date.setText(bean.date);
			holder.name.setText(bean.name);
			holder.name2.setText(bean.name);
			return convertView;
		}
	}

	private static class ViewHolder {

		public ImageView imageUrl;
		public ImageView logo;
		public TextView address;
		public TextView date;
		public TextView name;
		public TextView name2;
	}

	// -------------------优惠列表----------END

	@Override
	public void showprogressBar() {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateListView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onListViewListeners() {
		// more_listview.setOnItemSelectedListener(new OnItemSelectedListener()
		// {
		//
		// public void onItemSelected(AdapterView<?> mAdapterView,
		// View parent, int position, long id) {
		//
		// }
		//
		// public void onNothingSelected(AdapterView<?> arg0) {
		// }
		// });
		more_listview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> mAdapterView, View parent,
					int position, long id) {
				// 点击然后将信息显示在上面
				// 点击的选项有选中效果
				// 跳到影片界绍
				mMovieNewList.index = position;
				showMovieItem(mMovieNewList.mMovieNews.get(position));
				mMovieNewListAdapter.notifyDataSetChanged();
				// Intent intent = new Intent(mContext, MovieDesAct.class);
				// mContext.startActivity(intent);
			}

		});
		more_listview.setSelection(mMovieNewList.index);
//		youhui_more_listview
//				.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//					public void onItemSelected(AdapterView<?> mAdapterView,
//							View parent, int position, long id) {
//						mMovieNewList.youhui_more_listview_index = position;
//						youhui_more_listview.setSelection(position);
//					}
//
//					public void onNothingSelected(AdapterView<?> arg0) {
//					}
//				});
		youhui_more_listview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> mAdapterView, View parent,
					int position, long id) {
				// 跳到优惠页面

				Intent intent = new Intent(mContext, YouHuiAct.class);
				Bundle mBundle = new Bundle();
				mBundle.putSerializable("bean", mMovieNewList.mYouHuiNews
						.get(position));
				intent.putExtra("data", mBundle);
				mContext.startActivity(intent);
			}

		});
		youhui_more_listview
				.setSelection(mMovieNewList.youhui_more_listview_index);
	}
}